<script setup>
import { ref } from 'vue';
import axios from 'axios';
import { useRouter } from 'vue-router';

const router = useRouter();

const formData = ref({
  wechatId: '',
  userName: '',
  password: '',
  confirmPassword: ''
});

const errors = ref({
  wechatId: '',
  userName: '',
  password: '',
  confirmPassword: ''
});

const isLoading = ref(false);
const showSuccess = ref(false); // 新增成功状态

const validateForm = () => {
  let isValid = true;

  // 重置错误信息
  errors.value = {
    wechatId: '',
    userName: '',
    password: '',
    confirmPassword: ''
  };

  // 验证微信ID
  if (!formData.value.wechatId) {
    errors.value.wechatId = '请输入微信ID';
    isValid = false;
  }

  // 验证用户名
  if (!formData.value.userName) {
    errors.value.userName = '请输入用户名';
    isValid = false;
  } else if (formData.value.userName.length < 3) {
    errors.value.userName = '用户名至少需要3个字符';
    isValid = false;
  }

  // 验证密码
  if (!formData.value.password) {
    errors.value.password = '请输入密码';
    isValid = false;
  } else if (formData.value.password.length < 6) {
    errors.value.password = '密码长度至少为6位';
    isValid = false;
  }

  // 验证确认密码
  if (formData.value.password !== formData.value.confirmPassword) {
    errors.value.confirmPassword = '两次输入的密码不一致';
    isValid = false;
  }
  return isValid;
};

const handleSubmit = async () => {
  if (!validateForm()) return;

  isLoading.value = true;

  try {
    const response = await axios.post('http://localhost:8080/auth/register', {
      wechatId: formData.value.wechatId,
      userName: formData.value.userName,
      password: formData.value.password
    });

    if (response.data) {
      showSuccess.value = true; // 显示成功状态
      // 1.5秒后跳转到首页
      setTimeout(() => {
        router.push('/home');
      }, 1500);
    } else {
      alert('注册失败: ' + response.data.message);
    }
  } catch (error) {
    console.error('注册错误:', error);
    if (error.response && error.response.data) {
      alert('注册失败: ' + error.response.data.message);
    } else {
      alert('注册过程中发生错误');
    }
  } finally {
    isLoading.value = false;
  }
};
</script>

<template>
  <div class="register-container">
    <div class="register-card">
      <div class="register-header">
        <h2>用户注册</h2>
        <p>加入我们，开启全新体验</p>
      </div>

      <!-- 成功提示 -->
      <div v-if="showSuccess" class="success-message">
        <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none"
             stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
          <path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"></path>
          <polyline points="22 4 12 14.01 9 11.01"></polyline>
        </svg>
        <span>注册成功！正在跳转到首页...</span>
      </div>

      <form v-else @submit.prevent="handleSubmit" class="register-form">
        <div class="form-group">
          <label for="wechatId">微信ID</label>
          <input
              type="text"
              id="wechatId"
              v-model="formData.wechatId"
              placeholder="请输入您的微信ID"
              :class="{ 'input-error': errors.wechatId }"
          />
          <span class="error-message">{{ errors.wechatId }}</span>
        </div>

        <div class="form-group">
          <label for="userName">用户名</label>
          <input
              type="text"
              id="userName"
              v-model="formData.userName"
              placeholder="请输入用户名(至少3个字符)"
              :class="{ 'input-error': errors.userName }"
          />
          <span class="error-message">{{ errors.userName }}</span>
        </div>

        <div class="form-group">
          <label for="password">密码</label>
          <input
              type="password"
              id="password"
              v-model="formData.password"
              placeholder="请输入密码(至少6位)"
              :class="{ 'input-error': errors.password }"
          />
          <span class="error-message">{{ errors.password }}</span>
        </div>

        <div class="form-group">
          <label for="confirmPassword">确认密码</label>
          <input
              type="password"
              id="confirmPassword"
              v-model="formData.confirmPassword"
              placeholder="请再次输入密码"
              :class="{ 'input-error': errors.confirmPassword }"
          />
          <span class="error-message">{{ errors.confirmPassword }}</span>
        </div>

        <button type="submit" class="submit-btn" :disabled="isLoading">
          <span v-if="!isLoading">立即注册</span>
          <span v-else class="loading">注册中...</span>
        </button>
      </form>

      <div class="login-link">
        已有账号？
        <router-link to="/login">立即登录</router-link>
      </div>
    </div>
  </div>
</template>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  padding: 1rem;
  background-color: #f5f7fa;
}

.register-card {
  width: 100%;
  max-width: 420px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  padding: 2.5rem;
  transition: all 0.3s ease;
}

.register-header {
  text-align: center;
  margin-bottom: 2rem;
}

.register-header h2 {
  font-size: 1.75rem;
  color: #333;
  margin-bottom: 0.5rem;
  font-weight: 600;
}

.register-header p {
  color: #666;
  font-size: 0.95rem;
}

.success-message {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: 1rem;
  margin-bottom: 1.5rem;
  background-color: #f0f9eb;
  color: #67c23a;
  border-radius: 8px;
  font-weight: 500;
}

.success-message svg {
  color: #67c23a;
}

.register-form {
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

label {
  font-size: 0.9rem;
  color: #555;
  font-weight: 500;
}

input {
  padding: 0.85rem 1rem;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  font-size: 0.95rem;
  transition: all 0.2s;
}

input:focus {
  outline: none;
  border-color: #4a6cf7;
  box-shadow: 0 0 0 2px rgba(74, 108, 247, 0.1);
}

.input-error {
  border-color: #ff4d4f;
}

.input-error:focus {
  border-color: #ff4d4f;
  box-shadow: 0 0 0 2px rgba(255, 77, 79, 0.1);
}

.error-message {
  color: #ff4d4f;
  font-size: 0.8rem;
  min-height: 1.25rem;
  line-height: 1.25rem;
}

.submit-btn {
  width: 100%;
  padding: 0.9rem;
  background-color: #4a6cf7;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;
  margin-top: 0.5rem;
  transition: all 0.3s;
}

.submit-btn:hover {
  background-color: #3a5ce9;
}

.submit-btn:disabled {
  background-color: #94a3f8;
  cursor: not-allowed;
}

.loading {
  display: inline-block;
  animation: pulse 1.5s infinite;
}

@keyframes pulse {
  0% {
    opacity: 0.6;
  }
  50% {
    opacity: 1;
  }
  100% {
    opacity: 0.6;
  }
}

.login-link {
  text-align: center;
  margin-top: 1.5rem;
  font-size: 0.9rem;
  color: #666;
}

.login-link a {
  color: #4a6cf7;
  text-decoration: none;
  font-weight: 500;
  transition: color 0.2s;
}

.login-link a:hover {
  color: #3a5ce9;
  text-decoration: underline;
}

/* 响应式调整 */
@media (max-width: 480px) {
  .register-card {
    padding: 1.5rem;
  }

  .register-header h2 {
    font-size: 1.5rem;
  }

  input {
    padding: 0.75rem;
  }

  .submit-btn {
    padding: 0.8rem;
  }
}
</style>